<template>
  <div>
    <!-- 头部 -->
  <com-head title="商品列表" v-show="showHead"/>
  <!-- 排序 -->
  <div class="filter"  :style="{top:navTop}">
    <div
        @click="orderbySale"
        :class="['sale', 'order', {
        active: order.by === 'sale'
      }]">
        <span>销量</span>
        <div :class="['up', 'angle', {
          active:  order.by === 'sale' && order.updown === 0
        }]">
        </div>
        <div  :class="['down', 'angle', {
          active:  order.by === 'sale' && order.updown === 1
        }]">
        </div>
    </div>
      <div
        @click="orderbyPrice"
        :class="['price', 'order', {
        active: order.by === 'price'
      }]">
        <span>价格</span>
         <div :class="['up', 'angle', {
          active:  order.by === 'price' && order.updown === 0
        }]">
        </div>
        <div  :class="['down', 'angle', {
          active:  order.by === 'price' && order.updown === 1
        }]">
        </div>
      </div>
  </div>

    <!-- 商品列表 -->
    <div class="wrap2">
      <div class="items">
       <!-- 暂无商品 -->
          <div class="noitem" v-if="showNoitem">
            <img src="../../assets/img/zanwu.jpg" alt="">
          </div>

      <div class="item"
      v-for="item in items"
      :key="item.id"
      @click="enterDetail(item.id)"
      >
        <div class="item-left">
          <img :src="item.pic" alt="">
        </div>
        <div class="item-right">
          <h4 class="name">{{item.name}}</h4>
          <div >
            <span class="price">￥ {{item.minPrice}}</span>
           <!-- <span class="yuanjia">￥ {{item.originalPrice}}</span> -->
          </div>
            <div class="action">
            <span class="stores">销量：{{item.stores}}份</span>
            <van-icon color="#f66" size="24" class-prefix="icon" name="aixin"/>
          </div>
        </div>
      </div>
    </div>
    </div>
    <!-- /商品列表 -->
  </div>
</template>

<script>
import { fetchItems } from '@api'
import ComHead from '@components/ComHead'
export default {
  data () {
    return {
      order: {
        // 即打开默认按照 销量降序排序
        by: 'sale', // sale 按照销量 price按照价格
        updown: 1 // 0 升序 1降序
      },
      items: [],
      showHead: true,
      navTop: '46px',
      showNoitem: false
    }
  },
  created () {
    this.fetchItems()
  },
  mounted () {
    this.scrollHead()
  },
  methods: {
    enterDetail (id) {
      this.$router.push(`/detail/${id}`)
    },
    // 设置滚动函数
    scrollHead () {
      window.onscroll = () => {
        const hTop = document.documentElement.scrollTop || document.body.scrollTop
        if (hTop > 46) {
          this.showHead = false
          this.navTop = 0
        } else {
          this.showHead = true
          this.navTop = '46px'
        }
      }
    },
    // 请求商品 发送当前分类id和排序方式
    fetchItems () {
      fetchItems({
        categoryId: this.$route.query.cateId,
        orderBy: this.orderBy // 这个orderBy是计算属性中的
      }).then(res => {
        if (res.data.code === 0) {
          this.items = res.data.data.result
        }
        if (res.data.code === 700) {
          this.showNoitem = true
        }
      })
    },
    orderbySale () {
      // 首先判断 当前是否处于按照销量 不处于的话改为默认销量降序
      if (this.order.by !== 'sale') {
        this.order.by = 'sale'
        this.order.updown = 1
      } else {
        // 如果当前处于销量 点击改变 升降序
        if (this.order.updown === 0) {
          this.order.updown = 1
        } else {
          this.order.updown = 0
        }
      }
      this.fetchItems()
    },
    orderbyPrice () {
      // 首先判断 当前是否处于按照价格 不处于的话改为默认价格升序
      if (this.order.by !== 'price') {
        this.order.by = 'price'
        this.order.updown = 0
      } else {
        // 如果当前处于价格 点击改变 升降序
        if (this.order.updown === 0) {
          this.order.updown = 1
        } else {
          this.order.updown = 0
        }
      }
      this.fetchItems()
    }
  },
  computed: {
    // 计算属性 直接挂载到实例上 使用方法和data中的数据一样 所以不要和data，methods中的同名
    // 依赖于当前data中的数据 返回对应的接口 参数值 发送请求
    orderBy () {
      if (this.order.by === 'sale' && this.order.updown === 1) {
        return 'ordersDown'
      }
      if (this.order.by === 'sale' && this.order.udown === 0) {
        return 'ordersUp'
      }
      if (this.order.by === 'price' && this.order.updown === 0) {
        return 'priceUp'
      }
      if (this.order.by === 'price' && this.order.updown === 1) {
        return 'priceDown'
      }
      return 'ordersDown'
    }
  },
  components: {
    ComHead
  }
}
</script>

<style lang="scss" scoped>
.wrap2{
  margin-top: 86px;
  .noitem{
    img{
      width: 100%;
    }
  }
}
.filter{
  background-color: rgb(253, 246, 177);
    position: fixed;
    width: 100%;
  height: 40px;
  left: 0;
  display: flex;
  align-items: center;
  justify-content: space-around;

  .order{
    position: relative;
    &.active{
      color: #abb;
    }
  }
   .angle{
      width:0;
      height: 0;
      border: 4px solid transparent;
      position: absolute;
      right: -10px;
    }
    .up{
    // 上尖角
    border-bottom-color: #333;
    top:0;
    &.active{
      border-bottom-color: #abb;
    }
  }
  .down{
    // 下尖角
    border-top-color: #333;
    top:10px;
      &.active{
      border-top-color: #abb;
    }
  }
}

.item{
  height: 180px;
  display: flex;
  align-items: center;
  border-bottom: 1px solid grey;
  &-left{
    height: 180px;
    display: flex;
    align-items: center;
    padding-left: 10px;
    img{
      width: 140px;
      height: 140px;

    }
  }
  &-right{
    flex:1;
    height: 180px;
    padding:0 10px;
    box-sizing: border-box;
    .name{
      font-weight: 500;
      font-size: 18px;
      line-height: 46px;
      color:rgb(31, 30, 30);
    }
    .desc{
      line-height: 36px;
      color: #666;
    }
     .price{
        color: #ff2222;
    font-size: 18px;
      }
    .yuanjia {
     font-size: 16px;
     margin-left: 8px;
    color: gray;
    text-decoration: line-through;
  }
    .action{
        margin-top: 10px;
      display: flex;
      justify-content: space-between;
      padding-right: 20px;
      box-sizing: border-box;
      align-items: center;
      .stores{
          color: grey;
      }
    }
  }
}
</style>
